package org.hxyjs.dao.impl;

import org.hxyjs.dao.FestivalDao;
import org.hxyjs.entity.Festival;
import org.hxyjs.entity.Festival;
import org.hxyjs.entity.Festival;
import org.hxyjs.mapper.FestivalMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("festivalDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class FestivalDaoImpl implements FestivalDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private FestivalMapper festivalMapper;
    public List<Festival>  getAllFestivals() throws Exception{
        return festivalMapper.selectByExample(null);
    }

    @Override
    public Integer deleteFestivalByFno(String fno)  throws Exception{
        return festivalMapper.deleteByPrimaryKey(fno);
    }

    @Override
    public Integer updateFestivalByFno(Festival festival)  throws Exception{
        return festivalMapper.updateByPrimaryKey(festival);
    }

    @Override
    public Integer addFestival(Festival festival)  throws Exception{
        festival.setFno(initId());
        return festivalMapper.insertSelective(festival);
    }
    public String initId(){
        String maxFno = festivalMapper.getMaxFno();
        return GenNumTools.initId("",3,maxFno);
    }
    @Override
    public List<Festival> getFestivalByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)festivalMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return festivalMapper.getFestivalByPage(map);
    }
}
